<?php
namespace app\repair\model;
use think\Request;
use app\common\model\Common;
use think\Db;
class Repair extends Common
{
	protected $autoWriteTimestamp  = true;
	protected $createTime = 'addtime';
	protected $updateTime = false;
	protected $append = ['state_text','gname','admin_nkname','tech_nkname','shopname','is_prote_text','repair_log','describes','wg_des','gz_des','ratio_money'];

	/**
	 * 获取短信模版
	 */
	public function getMessage($type)
	{
		return Db::name('message')->where('type',$type)->select();
	}
	/**
	 * 获取维修总额
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function countRepairMoney($where, $order, $search = '')
	{
		return $this->where( $where )->where($search)->order($order)->sum('repair_money');
	}
	/**
	 * 获取店铺名字
	 * @param intger $shop_id 店铺id
	 */
	public function getShopname($shop_id){
		return Db::name('shop')->where('shop_id',session('admin.shop_id'))->value('shop_name');
	}
	/*添加时间获取器*/
	public function getAddtimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d H:i:s',$value) : '不存在';
    }
    /*状态获取器*/
    public function getStateTextAttr($value, $data)
    {
    	$state = [0=>'待确认',1=>'等待配件',2=>'等待送机',3=>'待维修',4=>'维修中',5=>'维修外送',6=>'待取机',7=>'退款并取机',8=>'维修完成',9=>'维修失败'];

    	return $data['cancel'] == 1 ? '维修取消' : $state[$data['state']];
    }
    /**
     * 产品名称获取器
     */
    public function getGnameAttr($value, $data)
    {
    	if($value != '') return $value;
    	return $data['goods_name'] . ' ' . $data['category_name'] . ' ' .$data['brand_name'] . ' ' .$data['marker'] . ' ' .$data['colour_name'];
    }
    public function getAdminNknameAttr($value, $data)
    {
    	return $this->getNkname($data['admin_id']);
    }
    public function getTechNknameAttr($value, $data)
    {
    	return $this->getNkname($data['technician_id']);
    }
    public function getShopnameAttr($value, $data)
    {
    	return Db::name('shop')->where('shop_id',$data['shop_id'])->value('shop_name');
    }
    public function getIsProteTextAttr($value, $data)
    {
    	switch($data['is_prote']){
    		case 0: return '保内';break;
    		case 1: return '保外';break;
    		default: return '异常';break;
    	}
    }
    public function getRepairLogAttr($value, $data)
    {
    	$rs = Db::name('repairLog')->where('state',0)->where('repair_id',$data['repair_id'])->select();
    	foreach($rs as $k => $v){
    		$rs[$k]['addtime'] = $this->date_time($v['addtime']);
    	}
    	return $rs;
    }
    public function getDescribesAttr($value)
    {
        return $value == '' ? '无' : $value;
    }
    public function getWgDesAttr($value, $data)
    {
        $value = '';
        if($data['wg_describe'] != ''){
            $value .= $data['wg_describe'];
        }
        if($data['wg_write_describe'] != ''){
            $value = $value == '' ? $data['wg_write_describe'] : $value.','.$data['wg_write_describe'];
        }   
        return $value;
    }
    public function getGzDesAttr($value, $data)
    {
        $value = '';
        if($data['gz_describe'] != ''){
            $value .= $data['gz_describe'];
        }
        if($data['gz_write_describe'] != ''){
            $value = $value == '' ? $data['gz_write_describe'] : $value.','.$data['gz_write_describe'];
        }   
        return $value;
    }
    public function getRatioMoneyAttr($value, $data)
    {
        $ratio = $this->repairRatio(session('admin.admin_type'), $data);
        return $this->royaltyMoney('0', $data['people_money'], $ratio);
    }
    /************************ 私有 **********************/
    
    /**
     * 获取管理员名字
     */
    private function getNkname($admin_id)
    {
    	$nkname = Db::name('Admin')->where('admin_id',$admin_id)->value('nkname');
    	return $nkname == '' ? '----' : $nkname;
    }
}